package com.block.source.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.db.entity.SystemIpAddressInfo;
import com.block.db.mapper.SystemIpAddressInfoMapper;
import com.block.source.service.ISystemIpAddressInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2024-05-16
 */
@Service
public class SystemIpAddressInfoServiceImpl extends ServiceImpl<SystemIpAddressInfoMapper, SystemIpAddressInfo> implements ISystemIpAddressInfoService {

    @Override
    public SystemIpAddressInfo getIpAddressInfo(String ip){

        SystemIpAddressInfo addressInfo = this.baseMapper.getEntityByIp(ip);
        if (ObjectUtil.isNotEmpty(addressInfo)){
            return addressInfo;
        }
        try {
            // 请求ip 信息
            SystemIpAddressInfo httpIpResult = this.getHttpIpResult(ip);
            boolean save = this.save(httpIpResult);
            return httpIpResult;
        }catch (Exception exceptione){
            exceptione.printStackTrace();
        }
        return null;
    }

    public SystemIpAddressInfo getHttpIpResult(String ip){

        String requestUrl = "https://ipaddr.market.alicloudapi.com/ip_addr_search/ch/v1?IP_ADDR="+ip;
        String result = HttpUtil.createGet(requestUrl)
                .header("Authorization", "APPCODE aff81d7303bb4573af4a83bfeada9683")
                .header("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
                .header("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
                .execute().body();

//        System.out.println(result);

        JSONObject jsonObject = JSON.parseObject(result);

        JSONObject infoObject = jsonObject.getJSONObject("IP地址解析实体信息");

        JSONObject ipObject = infoObject.getJSONObject("待查询IP地址解析实体信息");

        String provinceInfo = ipObject.getString("省份");
        String cityInfo = ipObject.getString("城市");
        String cityCode = ipObject.getString("国家行政编码");

        SystemIpAddressInfo addressInfo = new SystemIpAddressInfo();
        addressInfo.setIpInfo(ip);
        addressInfo.setAddressInfo(provinceInfo + "-" + cityInfo);
        addressInfo.setCodeInfo(cityCode);
        addressInfo.setResultInfo(result);

        return addressInfo;
    }

    public static void main(String[] args) {

        String requestUrl = "https://ipaddr.market.alicloudapi.com/ip_addr_search/ch/v1?IP_ADDR=61.164.109.178";
        String result = HttpUtil.createGet(requestUrl)
                .header("Authorization", "APPCODE aff81d7303bb4573af4a83bfeada9683")
                .header("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
                .header("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
                .execute().body();

        System.out.println(result);




    }


}
